/**
 * 
 */
package com.effectiv.req1.controller;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.effectiv.req1.business.api.ProjectService;
import com.effectiv.req1.domain.Project;
import com.effectiv.req1.domain.ProjectActivity;
import com.effectiv.req1.repository.ProjectActivityRepository;

/**
 * @author Dhrubo
 * 
 */
@Controller
@Slf4j
public class HomeController {
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private ProjectActivityRepository projectActivityRepository;
	
	@RequestMapping(value = "/home")
	public final String showHome(Model model) {
		
		//load the activities for the projects this user is part.
		List<Project> projects = projectService.findProjects();
		
		Sort sort = new Sort(Sort.Direction.DESC, "createdDate");
		
		List<ProjectActivity> activities = projectActivityRepository.findByProjectIsIn(projects, sort);
		
		
		log.info("Activities = {}", activities);
		
		model.addAttribute("activities", activities);
		
		return "home";
	}
	
	
}
